document.addEventListener('DOMContentLoaded', function() {

	const selectAllCheckbox = document.querySelector('.cart_list input[type="checkbox"]');
	const itemCheckboxes = document.querySelectorAll('.cart_item_check input[type="checkbox"]');
	const deleteButtons = document.querySelectorAll('.cart_item_action a');
	const cartItems = document.querySelectorAll('.cart_item');
	const totalPriceLink = document.querySelector('.cart_text a');
	const emptyCartMsg = document.querySelector('.cart_img');
	emptyCartMsg.style.display = 'none';
	
	
	selectAllCheckbox.addEventListener('change', function() {
		const isChecked = this.checked;
		itemCheckboxes.forEach(checkbox => {
			checkbox.checked = isChecked;
		});
		updateTotalPrice();
	});
	
	
	itemCheckboxes.forEach(checkbox => {
		checkbox.addEventListener('change', function() {
			const allChecked = Array.from(itemCheckboxes).every(cb => cb.checked);
			selectAllCheckbox.checked = allChecked;
			updateTotalPrice();
		});
	});


	deleteButtons.forEach(button => {
		button.addEventListener('click', function() {
			const cartItem = this.closest('.cart_item');
			cartItem.remove();
			updateTotalPrice();
			checkEmptyCart();
		});
	});


	document.querySelectorAll('.quantity_btn button').forEach(button => {
		button.addEventListener('click', function() {
			const input = this.parentElement.querySelector('input');
			let quantity = parseInt(input.value);

			if (this.textContent === '+' && quantity < 99) {
				quantity++;
			} else if (this.textContent === '-' && quantity > 1) {
				quantity--;
			}

			input.value = quantity;
			updateSubtotal(this.closest('.cart_item'));
			updateTotalPrice();
		});
	});


	document.querySelectorAll('.quantity_btn input').forEach(input => {
		input.addEventListener('change', function() {
			let quantity = parseInt(this.value);
			if (isNaN(quantity)) quantity = 1;
			if (quantity < 1) quantity = 1;
			if (quantity > 99) quantity = 99;
			this.value = quantity;
			updateSubtotal(this.closest('.cart_item'));
			updateTotalPrice();
		});
	});

    //更新单个商品小计金额
	function updateSubtotal(item) {
		const quantity = parseInt(item.querySelector('.quantity_btn input').value);
		const price = parseFloat(item.querySelector('.cart_item_price').textContent);
		const subtotal = (quantity * price).toFixed(2);
		item.querySelector('.cart_item_subtotal').textContent = subtotal;
	}


	function updateTotalPrice() {
		let total = 0;
		const currentItems = document.querySelectorAll('.cart_item');

		currentItems.forEach(item => {
			if (item.querySelector('.cart_item_check input').checked) {
				const subtotal = parseFloat(item.querySelector('.cart_item_subtotal').textContent);
				total += subtotal;
			}
		});

		totalPriceLink.textContent = '￥' + total.toFixed(2);
	}

     //控制空购物车提示显示
	function checkEmptyCart() {
		const items = document.querySelectorAll('.cart_item');
		if (items.length === 0) {
			emptyCartMsg.style.display = 'block';
			selectAllCheckbox.checked = false;
			totalPriceLink.textContent = '￥0.00';
		} else {
			emptyCartMsg.style.display = 'none';
		}
	}

	updateTotalPrice();
});
 //处理结算流程
function handleCheckout() {
	const selectedItems = [];
	document.querySelectorAll('.cart_item').forEach(item => {
		if (item.querySelector('input[type="checkbox"]').checked) {
			selectedItems.push({
				id: item.dataset.id,
				name: item.querySelector('.cart_item_name').textContent,
				quantity: parseInt(item.querySelector('input[type="number"]').value),
				price: parseFloat(item.querySelector('.cart_item_price').textContent.replace('¥', '')),
				image: item.querySelector('.cart_item_img').src
			});
		}
	});

	if (selectedItems.length === 0) {
		alert('请选择要结算的商品');
		return;
	}

	sessionStorage.setItem('selectedCartItems', JSON.stringify(selectedItems));
	window.location.href = 'orders.html';
}